package com.example.hao.flowercityfm.HFAdapter.ViewHolder;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.hao.flowercityfm.R;
import com.example.hao.flowercityfm.application.MyApp;
import com.example.hao.flowercityfm.pojo.Type5ListItem;
import com.example.hao.flowercityfm.pojo.Type6ListItem;
import com.example.hao.flowercityfm.utils.HttpUtils;
import com.google.gson.Gson;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Type6ViewHolder extends BaseTypeViewHolder {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rv)
    RecyclerView rv;
    private Type6RvAdapter type6RvAdapter;

    public Type6ViewHolder(Context context, View itemView) {
        super(context, itemView);
        initView();
    }

    @Override
    public void initView() {
        title.setText("为你推荐");
        rv.setLayoutManager(new LinearLayoutManager(context));
        type6RvAdapter = new Type6RvAdapter(context);
        rv.setAdapter(type6RvAdapter);
        loadData(URL_18);
    }

    private static final String URL_18 = "https://api.myjson.com/bins/mvw7k";

    @Override
    void loadData(String url) {
        HttpUtils.method2(url, new HttpUtils.OnOkCallBack() {
            @Override
            public void onFailure(@NonNull IOException e) {
                Log.e(MyApp.TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(@NonNull String jsonStr) {
                Type6ListItem type6ListItem = new Gson().fromJson(jsonStr, Type6ListItem.class);
                type6RvAdapter.setItem(type6ListItem);
            }
        });
    }

    class Type6RvAdapter extends RecyclerView.Adapter<Type6RvAdapter.ViewHolder> {


        private Context context;
        private Type6ListItem type6ListItem;

        public Type6RvAdapter(Context context) {
            this.context = context;
        }

        public void setItem(Type6ListItem type6ListItem) {
            this.type6ListItem = type6ListItem;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.fragment_home_type6_item, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Type6ListItem.DataBean.PgcAudioVideoListBean bean = type6ListItem.getData().getPgcAudioVideoList().get(position);
            holder.tvName.setText(bean.getName());
            holder.tvInformation.setText(bean.getIntro());
            Glide.with(context).load(bean.getPictureUrl()).into(holder.ivBg);
            holder.countClickSum.setText(String.valueOf(bean.getCountClickSum()));
            holder.updateDate.setText(bean.getUpdateDate());
        }

        @Override
        public int getItemCount() {
            return type6ListItem == null ? 0 : type6ListItem.getData().getPgcAudioVideoList().size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.iv_bg)
            ImageView ivBg;
            @BindView(R.id.tv_name)
            TextView tvName;
            @BindView(R.id.tv_information)
            TextView tvInformation;
            @BindView(R.id.countClickSum)
            TextView countClickSum;
            @BindView(R.id.updateDate)
            TextView updateDate;

            public ViewHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
